using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.PageFlow;

namespace WCSFContrib.PageFlow.Xml.Storage.Session
{
    public class PageFlowInstaceSessionTracker
    {
        private Guid runningInstance = Guid.Empty;
        private Dictionary<Guid, string> instances = new Dictionary<Guid, string>();

        public void Add(IPageFlow pageFlow)
        {
            instances.Add(pageFlow.Id, pageFlow.Definition.PageFlowType.AssemblyQualifiedName);
        }

        public void Remove(Guid pageFlowId)
        {
            if (RunningInstance == pageFlowId)
                RunningInstance = Guid.Empty;
            instances.Remove(pageFlowId);
        }

        public string GetInstanceType(Guid pageFlowId)
        {
            return instances[pageFlowId];
        }

        public bool Contains(Guid pageFlowId)
        {
            return instances.ContainsKey(pageFlowId);
        }

        public Guid GetByType(Type pageFlowType)
        {
            string assemblyQualifiedName = pageFlowType.AssemblyQualifiedName;
            foreach (KeyValuePair<Guid, string> instance in instances)
            {
                if (instance.Value == assemblyQualifiedName)
                    return instance.Key;
            }
            return Guid.Empty;
        }


        public Guid RunningInstance
        {
            get { return runningInstance; }
            set { runningInstance = value; }
        }

    }
}
